<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>楼层导航</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            font-size: 50px;
            width: 1200px;
            height: 800px;
            background-color: skyblue;
            margin: 20px auto;
            /* margin-bottom: 30px; */

        }

        ul li {
            cursor: pointer;
        }

        ul {
            list-style: none;
            font-size: 30px;
            position: fixed;
            width: 100px;
            height: 250px;
            background-color: orange;
            right: 52px;
            top: 50%;
            margin-top: -25px;
            text-align: center;
        }

        ul li.cur {
            background-color: red;
        }
    </style>
</head>

<body>
<section>
    <div class="c" data-n="吃">吃</div>
    <div class="c" data-n="喝">喝</div>
    <div class="c" data-n="玩">玩</div>
    <div class="c" data-n="乐">乐</div>
    <div class="c" data-n="拉">拉</div>
    <div class="c" data-n="撒">撒</div>
</section>
    <ul id="list">
        <li class="" data-n="吃">吃</li>
        <li data-n="喝">喝</li>
        <li data-n="玩">玩</li>
        <li data-n="乐">乐</li>
        <li data-n="拉">拉</li>
        <li data-n="撒">撒</li>
    </ul>

    <script>
        var list = document.getElementById('list');
        list.onclick = function (e) {
            if (e.target.tagName.toLowerCase() == 'li') {
                var n = e.target.getAttribute('data-n');
                var c = document.querySelector('.c[data-n=' + n + ']');
                document.documentElement.scrollTop = Math.ceil(c.offsetTop);
                console.log(document.documentElement.scrollTop);
                console.log(c.offsetTop);
            }
        }
        var c = document.querySelectorAll('.c');
        var gu = document.querySelectorAll('#list li');
        var op = [];
        var nowfool = -1;
        for (var i = 0; i < c.length; i++) {

            op.push(c[i].offsetTop);
        }
        op.push(Infinity)
        console.log(op);
        window.onscroll = function () {
            var gaod = document.documentElement.scrollTop;
            for (var i = 0; i < c.length; i++) {
                if (gaod >= op[i] && gaod < op[i + 1]) {
                    for (var j = 0; j < gu.length; j++) {
                        if (j == i) {
                            gu[j].className = 'cur';
                        } else  {
                            gu[j].className = '';
                        }

                    }
                }
            }
        }

    </script>
</body>

</html>